package com.dogcatfishdish.anaglyph
{
	import flash.display.BlendMode;
	import flash.display.Sprite;
	import flash.filters.DropShadowFilter;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	
	public class TextBoxZ extends Sprite 
	{
		private static const OFFSET_FACTOR:Number = 1;
		
		[Embed(source="swissb.ttf", mimeType="application/x-font", fontName="Swiss721BT-Roman", embedAsCFF="false", fontStyle="normal")] 
		public var __font:Class;
		
		public var red:TextField;
		public var green:TextField;
		
		private var dir:Boolean;
		
		public function TextBoxZ(z:Number)
		{
			super();
			this.z = z;
			
			blendMode = BlendMode.LAYER;
			
			dir = Boolean(Math.random() * 2 | 0);
			red = createTextField(0);
			green = createTextField(2);
			
			graphics.beginFill(0xFFFFFF);
			
		}
		
		private function createTextField(type:int):TextField {
			
			var tf:TextField = new TextField();
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.antiAliasType = AntiAliasType.NORMAL;
			tf.multiline = false;
			tf.embedFonts = true;
			tf.defaultTextFormat = new TextFormat('Swiss721BT-Roman', 24, 0xFF0000);
			
			var off:Number = z * OFFSET_FACTOR;

			tf.alpha = 1;
			tf.blendMode = BlendMode.SCREEN;
			tf.text = 'Hello world!';
			switch(type) {
				case 0:
					tf.textColor = 0xFF0000;
					tf.x = -off - tf.width * 0.5;
					break;
				case 2:
					tf.textColor = 0x00ffff;
					tf.x = off - tf.width * 0.5;
					break;
			}
			tf.y = -tf.textHeight * 0.5;
			tf.filters = [new DropShadowFilter(0, 90, 0, 1)];
			addChild(tf);
			return tf;
		}
		
		public function update():void {
			if(z < 0) {
				dir = true;
			}
			if(z > 300) {
				dir = false;
			}
			
			if(dir) {
				z += 1;
			} else {
				z -= 1;
			}
			
			var off:Number = z * OFFSET_FACTOR;
			red.x = -off - red.width * 0.5;
			green.x = off - green.width * 0.5;
		}
	}
}